package com.preresearch.recyclerview;

import android.app.Application;
import android.content.Context;

import com.nostra13.universalimageloader.cache.disc.impl.ext.LruDiskCache;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.preresearch.recyclerview.util.ImageLoaderUtils;

import java.io.File;

/**
 * @author carlos
 * @date 2016/5/27
 */
public class MyApplication extends Application {

    private static final String IMAGE_CACHE_DIR_NAME = "imageCache";
    private static Context mAppContext;

    @Override
    public void onCreate() {
        super.onCreate();
        mAppContext = this;
        // 初始化图片加载框架.
        initImageLoader();
    }

    /**
     * 初始化 UIL图片加载框架
     */
    private void initImageLoader() {
        // 缓存路径:
        String imageDiskCachePath = ImageLoaderUtils.getDiskCachePath(getApplicationContext(), IMAGE_CACHE_DIR_NAME);

        File imageDiskCacheDir = ImageLoaderUtils.makeAndGetImageDiskCacheDir(imageDiskCachePath);

        try {
            ImageLoaderConfiguration.Builder configBuilder = new ImageLoaderConfiguration.Builder(this)
                    .threadPoolSize(5)// 线程池内加载的数量
                    .threadPriority(Thread.NORM_PRIORITY - 2)
                    // 同一张图片的多个尺寸的图只存储一个, 不会全部存储
                    .denyCacheImageMultipleSizesInMemory()

                    /* memoryCache() and memoryCacheSize() calls overlap(重复) each other */
                    // 以下几个方法不能同时设置: memoryCache() 和 memoryCacheSize()
                    // 内存缓存容量2MB
                    .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
                    // 设置线程池中的图片队列为 LIFO队列, 即: 优先加载最后打开或者最后呈现出来的图片
                    .tasksProcessingOrder(QueueProcessingType.LIFO)
                    /* diskCache() and diskCacheFileNameGenerator() calls overlap each other */
                    /* diskCache(), diskCacheSize() and diskCacheFileCount() calls overlap each other */
                    // 以下几个方法不能同时设置: diskCache(), diskCacheSize() and diskCacheFileCount(), diskCacheFileNameGenerator()
                    // 硬盘缓存容量50MB
                    .diskCache(new LruDiskCache(imageDiskCacheDir, new Md5FileNameGenerator(),
                            1L * 50 * 1024 * 1024))
                    // 连接超时时间5s, 读取超时时间30s
                    .imageDownloader(new BaseImageDownloader(this, 5 * 1000, 30 * 1000));

            // 只有在DEBUG模式下, 才会开启图片加载框架 UIL的调试log开关.
                configBuilder.writeDebugLogs();
            ImageLoaderConfiguration config = configBuilder.build();
            // 初始化图片加载框架 UIL
            ImageLoader.getInstance().init(config);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}